package com.soft.mainsite.module.content.beetl;

import com.soft.mainsite.common.constant.CmsConst;
import com.soft.mainsite.common.utils.StrUtil;
import com.soft.mainsite.module.category.service.TCmsCategoryService;
import com.soft.mainsite.module.web.system.service.RoleService;
import com.soft.mainsite.mybatis.model.TCmsCategory;
import org.apache.commons.lang3.ArrayUtils;
import org.beetl.core.Context;
import org.beetl.core.Function;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * Description:主站栏目创建选择全局后,子站点内容管理生成对应栏目结构树
 *
 * @author liu
 * @create 2019-04-16
 **/
@Service
public class ViewShowTreeCategoryAutomaticFunction implements Function{
    @Autowired
    private TCmsCategoryService categoryService;

    @Autowired
    private RoleService roleService;

    @Value("${system.http.protocol}")
    private String httpProtocol;

    @Value("${system.http.host}")
    private String httpHost;


    private String url = "/system/cms/category/input?id";

    @Override
    public Object call(Object[] objects, Context context) {
        Long pid = Long.parseLong(objects[0].toString());
        Integer siteId = Integer.parseInt(objects[1].toString());
        if(objects[2] != null && !StrUtil.isBlank(objects[2].toString())) {
            this.url = objects[2].toString();
        }
        return recursion(pid,siteId);
    }

    /**
     * 递归获取内容管理栏目菜单树
     * @param pid
     * @param siteId
     * @return
     */
    private String recursion(Long pid,Integer siteId){
       StringBuffer sbf = new StringBuffer();

       List<TCmsCategory> cats  = categoryService.findCategoryListByPid(pid,siteId);
       if(cats != null && cats.size() > 0){
           // 获取排除ids数据
           String[] idsArray = CmsConst.EX_CATEGORY.split(",");
           for(TCmsCategory cat:cats){
               // 判断是否是主站栏目ids
               if(!CmsConst.MAIN_SITE_ID.equals(siteId) && ArrayUtils.contains(idsArray,cat.getCategoryId().toString())){
                   continue;
               }
               int childSize = categoryService.findCategoryListByPid(cat.getCategoryId(),siteId).size();
               if((cat.getAlone()||(!StrUtil.isBlank(cat.getUrl()))&&childSize==0)) {
                   continue;
               }else if(childSize>0) {
                   sbf.append("  <li data-id=\"" + cat.getCategoryId() + "\" data-pid=\"" + pid + "\" >" + cat.getCategoryName() + " [" + cat.getCategoryId() + "] </li>");
               }else {
                   sbf.append("  <li data-id=\"" + cat.getCategoryId() + "\" data-pid=\"" + pid + "\" data-url=\"" + httpProtocol+"://"+ httpHost + url + "=" + cat.getCategoryId() + "\" data-divid=\"#layout-content-view-show\">" + cat.getCategoryName() + " [" + cat.getCategoryId() + "] </li>");
               }
               sbf.append(recursion(cat.getCategoryId(),siteId));
           }
           return  sbf.toString();
       }
       return "";
    }
}
